var util = require('../../utils/util.js')
var config = require('../../config')
var network = require("../../utils/network.js")
var md5 = require('../../utils/md5.js')
var payWeChat = require('../../utils/payWeChat.js')
var app = getApp()


//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusType: ["全部", "待付款", "待发货", "待收货", "已完成"],
    orderStatus: ["已完成","待付款","确认中","已发货","已签收","已结算","拒收","退货","已取消","待发货","假单","无效重复","返款异常","无法接通","拒接","停机","关机","空号",],// "待付款", "待发货", "待收货", "已完成"],
    currentType: 0,
    pagesize: 5,
    orderList: [],
    userid: app.globalData.userInfo.userid
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  showOrderList: function (zt, pagesize) {
    wx.showLoading()
    this.http(config.service.orderList, zt,this.bannerData)
  },

  http: function (url,zt, callback) {
    var that = this;
    wx.getStorage({
      key:'userInfo',
      success:function(res){
        var userInfoStorage = res.data;
        if (!util.isNUll(userInfoStorage)&&!util.isNUll(userInfoStorage.userid)) {
          app.globalData.userInfo = userInfoStorage;
         var userid =  userInfoStorage.userid;
          console.log("userid="+userInfoStorage);
          that.setData({
            userid:userid,
          })
          wx.request({
            url: url,
            data: {
              zt: zt,
              userid: that.data.userid

            },
            method: 'POST',
            header: {'content-type': 'application/x-www-form-urlencoded'},
            success: function (res) {
              wx.hideLoading()
              console.log(JSON.stringify(res));
              if(res.data.code==0){
                callback(res.data.data)
              }else{
                callback('')
              }
            }

          })
        }else{
          that.login();
        }

      }});

  },
  login: function (callback) {
    var that = this;
    app.login(function(e){
      wx.getStorage({
        key:'userInfo',
        success:function(res){
          console.log('getStorage----'+JSON.stringify(res));
          let userInfo = res.data;
          app.globalData.userInfo = userInfo;
          that.setData({
            userid:userInfo.userid,
            hasUserInfo: true,
          })
        }})})


      },

  bannerData: function (data) {
    this.setData({
      orderList: data.length?data:[]
    })
  },

  onNavBarTap: function (e) {
    var idx = e.currentTarget.dataset.index
    this.setData({
      pagesize: 5,
      currentType: idx
    })
    this.showOrderList(idx, this.data.pagesize)
  },

  onDetailTap: function (e) {
    var item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pages/orders-detail/index?id=' + JSON.stringify(item)
    })
  },
  // 取消订单
  cancelOrderTap: function (e) {
    var _this = this
    var order = e.currentTarget.dataset.item

    wx.showModal({
      title: '您将取消本次下单',
      content: '确认取消吗?',
      success(res) {
        if (res.confirm) {

          //写入参数
          var params =  {
            'orderid':order.id,
            'ordernum':order.ordernum,
            'userid':_this.data.userid,
            'zt':8
          };
          //发起请求
          network.POST(
              {
                url:config.service.orderUpdate,
                params:params,
                success: function (res) {
                  if (res.data.code == 0) {
                    _this.showOrderList(_this.data.currentType, _this.data.pagesize)
                  } else {
                    wx.showModal({
                      title: '提示',
                      content: res.data.msg,
                      showCancel: false
                    })
                  }
                },
                fail: function () {
                  //失败后的逻辑
                },
              })
        }
      }
    })
  },
  // 确认收货
  checkOrder: function (e) {
    var _this = this
    var orderId = e.currentTarget.dataset.orderid

    wx.showModal({
      title: '提示',
      content: '确认已经收货了吗？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.url + 'confirmOrder',
            data: {
              mid: app.globalData.userInfo.mid,
              order_id: orderId
            },
            success: (res) => {
              if (res.data.code == 1) {
                _this.onShow()
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.msg,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },
  /**
   * 微信支付
   */
  toPayTap: function (e) {
    var goods = e.currentTarget.dataset.goods;
    console.log('toPayTap---'+JSON.stringify(goods));
      var that = this;
      payWeChat.payOrder(app.globalData.appid, goods.nonceStr, 'prepay_id=' + goods.prepay_id,goods.amount, goods.ordernum,function(e){
      if(e=='success'){
          that.showOrderList(that.data.currentType, that.data.pagesize)
      }
    });




  }
    ,
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.showOrderList(this.data.currentType, this.data.pagesize)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.showOrderList(this.data.currentType, this.data.pagesize)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})